#!/bin/sh

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

#
# udev	This is a minimal non-LSB version of a UDEV startup script.  It
#	was derived by stripping down the udev-058 LSB version for use
#	with buildroot on embedded hardware using Linux 2.6.12+ kernels.
#
#	You may need to customize this for your system's resource limits
#	(including startup time!) and administration.  For example, if
#	your early userspace has a custom initramfs or initrd you might
#	need /dev much earlier; or without hotpluggable busses (like USB,
#	PCMCIA, MMC/SD, and so on) your /dev might be static after boot.
#
#	This script assumes your system boots right into the eventual root
#	filesystem, and that init runs this udev script before any programs
#	needing more device nodes than the bare-bones set -- /dev/console,
#	/dev/zero, /dev/null -- that's needed to boot and run this script.
#

# Directory where sysfs is mounted
SYSFS_DIR=/sys

case "$1" in
	start)
		echo -n "Starting UDEV ..."
		# Check for config file and read it
		UDEV_CONFIG=/etc/udev/udev.conf
		test -r $UDEV_CONFIG || exit 1
		. $UDEV_CONFIG

		# Check for missing binaries
		UDEV_BIN=/lib/udev/udevd
		test -x $UDEV_BIN || exit 1
		UDEVADM_BIN=/usr/bin/udevadm 
		test -x $UDEVADM_BIN || exit 1

		# Start kernel logging. It might catch some traces

		/sbin/klogd -c 2 2>/dev/null

		# mount sysfs if it's not yet mounted

		if [ ! -d $SYSFS_DIR ]; then
		echo "${0}: SYSFS_DIR \"$SYSFS_DIR\" not found"
		exit 1
		fi

		mount -t sysfs /sys /sys || exit 1

		mkdir $udev_root/pts $udev_root/shm

		# populate /dev (normally)
		echo -n "Populating $udev_root using udev: "
		$UDEV_BIN --daemon >/dev/null 2>/dev/null ||  exit 1
		mount -t devpts /dev/pts /dev/pts 2>/dev/null||  exit 1
		mount -t tmpfs none /dev/shm 2>/dev/null||  exit 1 

		# tmp
		umount -l /tmp
		#mount -t tmpfs tmpfs /tmp

		# Montamos el soporte para USB
		mount -t usbfs usbfs /proc/bus/usb/

		# Force detect coldplug devices
		$UDEVADM_BIN control --reload  >/dev/null
		$UDEVADM_BIN control --start-exec-queue  >/dev/null 
		$UDEVADM_BIN trigger  >/dev/null 
		
		# Leave a few seconds for auto-detection rules
		sleep 3
		exit 0
		;;
	stop)
		# Stop execution of events and udevd

		$UDEADM_BIN control --stop_exec_queue
		killall udevd 2>/dev/null

        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac


exit 0
